<style>
  @import '../../../static/style/siteDetail.css';
</style>
<template>
  <div class="wrapper" id="main" style="position: absolute; z-index: 200;top: 14px;width: auto">
    <div class="selectModelType">
      <div class="tip"><p>功能面板 | 地图量测</p></div>
      <el-button type="primary" size="mini" plain @click="init('line')">距离</el-button>
      <el-button type="primary" size="mini" plain @click="init('polygon')">面积</el-button>
      <el-button type="primary" size="mini" plain @click="markIt()">标记</el-button>
      <br><br>
      <el-button type="primary" size="mini" plain @click="dynamicCircle()">扩散</el-button>
      <el-button type="primary" size="mini" plain @click="diyPopup()">自定义Poup</el-button>

    </div>
    <div class="total-popup-content" style="position:absolute;display:none;">
      <div class="tip-layer">0</div>
      <img src="../../data/image/close-1.png" @click="clear()" style="width: 12px; height: 12px;vertical-align: middle">
<!--      <svg class="close" viewBox="0 0 45 35" @click="clear()">-->
<!--        <path class="close" style="stroke:#fff; strokeWidth:7" d="M 10,10 L 30,30 M 30,10 L 10,30" ></path>-->
<!--      </svg>-->
    </div>

  </div>
</template>


<script>
  import Vue from "vue";
  import CreateMarker_pop from '../widget/CreateMarker_pop';
  // import "src/components/widget/leaflet/leaflet-measure-path.css";
  // import "src/components/widget/leaflet/leaflet-measure-path.js";
  import CustomPopup from "../widget/related-jsFile/customPopup";

  import measureUtil from "../../script/cesiumFunction/cesiumMeasureUtils"
  import {measurePolyline,measurePolygon,clearTemps} from "../../script/leafletFunction/leafletMeasureUtils"
  export default {
    name: "measureEndback",
    data(){
      return{
        curMarker:null
      }
    },
    components:{
      // myViewer
    },
    mounted() {
      if(this.$store.state.currentFrameNameStore === "cesium"){
        viewer.camera.setView({
          destination:Cesium.Cartesian3.fromDegrees(113.524,34.819,5300),
          orientation: {
            heading:Cesium.Math.toRadians(360),
            pitch:Cesium.Math.toRadians(-90),
            roll: Cesium.Math.toRadians(0), //默认值8
          },
        });
        viewer.scene.mode = Cesium.SceneMode.SCENE2D;
        viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK)
      }
    },
    methods:{
      diyPopup(){
        leafletMap.once('click', this.onMapClick);
      },

      onMapClick(e) {
        //点位的信息
        let markerInfo = {
          name:'',
          notes:'',
          url:'static/image/marker/marker-icon.png',
          latlng:e.latlng
        };
        this.curMarker = this.createIconMarker(markerInfo);
        //放置自定义popup
        this.createPopup(this.curMarker);//marker._message.url
      },
      createPopup(marker) {
        let popUpLayer = new CustomPopup({
          className: "leaflet-custom-popup",
          // maxWidth: 400,
          offset: L.point(0, -35)//向上偏移
        }).setLatLng(marker._message.latlng).setContent("<div id='leafletPopupContainer'></div>");
        popUpLayer.openOn(leafletMap);
        let Toast = Vue.extend(CreateMarker_pop);//Vue.extend创建构造器，参数为一个vue组件
        let popupContent = new Toast();
        popupContent.$mount("#leafletPopupContainer");//把PromptBoxLeaflet组件挂载到#leafletPopupContainer中
        popupContent.openPrompt(marker);
        popupContent.$on("click-close", () =>{
          // if (this.popupLayer && this.popupLayer.isOpen()) {
            leafletMap.closePopup(popUpLayer);
          // }
        });
        popupContent.$on("save-change", (e) =>{
          //无法动态给marker点加图标，需要移除再添加
          leafletMap.removeLayer(this.curMarker);
          this.curMarker = this.createIconMarker(e);
        });
      },
      createIconMarker(message){
        let newIcon = L.icon({
          iconUrl: message.url,
          iconSize: [24,40],
          iconAnchor:[12,40],//使大小为 [24,40]的位于准确位置
        });
        let newMarker = L.marker([message.latlng.lat, message.latlng.lng],
          {title:"test-title",
            icon:newIcon,
            draggable:true,
            iconSize: [25,40],
            riseOnHover:true}).addTo(leafletMap);
        newMarker._message = message;
        newMarker.id = 'gisleung';


        newMarker.on('click', (ev) =>{
          // const {sourceTarget} = ev;
          this.createPopup(ev.sourceTarget,ev.sourceTarget._message.url);
        });
        return newMarker;
      },

      dynamicCircle(){
        leafletMap.once('click', onMapClick);
        function onMapClick(e) {
          let color = 'rgba(0, 145, 255, 1)'
          let htmlMarker = '<div class="siteDetail-circle-contain">' +
            '<i class="siteDetail-static-circle" style="border-color: ' + color +'"></i>' +
            '<i class="siteDetail-dynamic-circle01" style="border-color: ' + color +'"></i>' +
            '<i class="siteDetail-dynamic-circle02" style="border-color: ' + color +'"></i>' +
            '</div>';

          let myIcon = L.divIcon({className: 'leaflet-rect-marker', html: htmlMarker, iconSize: [80, 80]})
          let marker = L.marker([e.latlng.lat, e.latlng.lng],
            {icon: myIcon,
              iconSize: [80, 80],
              // interactive: false,//鼠标交互，默认为true
              title:"动态扩散圈",
              riseOnHover:true
            }).addTo(leafletMap);//
        }
      },
      markIt(){
        function onMapClick(e) {
          //点坐标
          let marker2 = L.marker([e.latlng.lat, e.latlng.lng],
            {title:"test-title",
              riseOnHover:true}).addTo(leafletMap);
          //弹窗信息
          let divHtml2 = "<b>the popup location is:</b><br>经度："+e.latlng.lng.toFixed(3)+" 维度："+e.latlng.lat.toFixed(3);
          marker2.bindPopup(divHtml2).openPopup();


          // 用html的div的样式来创建marker的icon，注意需要加className属性
          let icon3 = L.divIcon({
            html: "<div style='width:24px;height:24px;border-radius:4px;background-color:#00e400 ;'></div>",
            className: 'ss',
            iconSize: [24, 24]});
          let marker3 = L.marker([e.latlng.lat, e.latlng.lng+0.2], { icon: icon3, iconSize: [24, 24]}).addTo(leafletMap);
          //弹窗信息
          let divHtml3 = "<b>the popup location is:</b><br>经度："+e.latlng.lng.toFixed(3)+" 维度："+e.latlng.lat.toFixed(3);

          // marker3.bindPopup(divHtml3).openPopup();//自动打开
          // marker2.bindTooltip("my tooltip text2").openTooltip();//自动打开
          marker3.bindPopup(divHtml3);
          marker3.bindTooltip("my tooltip");

          //圆形标记
          L.circleMarker([e.latlng.lat, e.latlng.lng+0.4],{radius:10}).addTo(leafletMap);



          let divDom4 = document.createElement("div");
          divDom4.style.width = 11 + "px";
          divDom4.style.height = 11 + "px";
          divDom4.style.backgroundColor = 'rgb(255 0 0)';
          divDom4.style.borderRadius = "50%";
          divDom4.style.border = "1px solid";

          let myIcon4 = L.divIcon({html: divDom4,className: 'leaflet-circle-marker'});
          let marker4 = L.marker([e.latlng.lat, e.latlng.lng+0.6],
            {icon: myIcon4,
              iconSize: [12, 12],
              title:"自定义小圆点",
              riseOnHover:true
            }).addTo(leafletMap);


          let latlngs = [
            [e.latlng.lat, e.latlng.lng+0.2],
            [e.latlng.lat, e.latlng.lng+1],
          ];
          L.polyline(latlngs, {color: 'red'}).addTo(leafletMap);
          }
        leafletMap.once('click', onMapClick);

      },
      init(drawingMode){
        if(this.$store.state.currentFrameNameStore === 'cesium'){
          measureUtil.draw(drawingMode);//   line\polygon
        }else if(this.$store.state.currentFrameNameStore === 'leaflet'){
          drawingMode === "line" ? measurePolyline() :  measurePolygon();
        }
      },
      clear(){
        clearTemps();
      }
    },
    destroyed() {
      // 移除监听地图点击事件
      leafletMap.off("click");
      leafletMap.off("mousemove");
      leafletMap.off("dblclick");
      clearTemps();
      // mapBaseF.removeAllNtt()
    }
  }
</script>

<style scoped>
  .total-popup-content{
    font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
    user-select: none;
    cursor: pointer;
    pointer-events: auto;
    padding: 1px 7px;
    border-radius: 8px;
    font-weight: bold;
    white-space: nowrap;
    text-align: left;
    background-color: #FF0080;
    color: #fff;
  }
  .tip-layer{
    font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
    user-select: none;
    cursor: pointer;
    pointer-events: auto;
    font-weight: bold;
    white-space: nowrap;
    text-align: left;
    color: #fff;
    width: auto;
    min-width: 50px;
    display: inline-block;
  }
  .close{
    font: 12px/1.5 "Helvetica Neue", Arial, Helvetica, sans-serif;
    user-select: none;
    cursor: pointer;
    pointer-events: auto;
    font-weight: bold;
    white-space: nowrap;
    text-align: left;
    color: #fff;
    width: 15px;
    height: 10px;
    position: relative !important;
    top: 0px !important;
    left: 5px !important;
    z-index: 200;
  }
  .total-popup-content svg {
    width: 15px;
    height: 10px;
    position: relative !important;
    top: 0px !important;
    left: 5px !important;
  }


</style>
